// ignore: must_be_immutable
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:kmxy_base/util/mic_util.dart';

// ignore: must_be_immutable
class MicView extends StatefulWidget {
  late Function micStop;

  MicView(
    this.micStop, {
    super.key,
  });

  @override
  // ignore: library_private_types_in_public_api
  _MicViewState createState() => _MicViewState();
}

class _MicViewState extends State<MicView> {
  bool isPressRecord = false;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Container(
        padding: EdgeInsets.only(bottom: 10.w),
        height: 100.w,
        color: Colors.transparent,
        child: GestureDetector(
          child: SizedBox(
            width: MediaQuery.of(context).size.width,
            child: Stack(
              alignment: Alignment.center,
              children: [
                Image.asset(
                  isPressRecord ? 'assets/mic_gif.gif' : 'assets/mic_n.png',
                  fit: BoxFit.cover,
                ),
                isPressRecord
                    ? Image.asset(
                        'assets/mic_ico.png',
                        fit: BoxFit.cover,
                        width: 72.w,
                        height: 72.w,
                      )
                    : Container(),
              ],
            ),
          ),
          onLongPress: () async {
            await MicUtil.micStart();
            isPressRecord = MicUtil.isRecording;
            setState(() {});
          },
          onLongPressEnd: (details) {
            MicUtil.micStop((micPath, time) {
              isPressRecord = MicUtil.isRecording;
              setState(() {});
              widget.micStop(micPath, time);
            });
          },
        ),
      ),
    );
  }
}
